<?php require_once('../src/session_start.inc.php'); ?>
<?php
require_once('../src/define.inc.php');
require_once('../src/functions.php');
require_once('../src/annonce.class.php');
require_once('../src/alerte_mail.class.php');
//npr($_POST);

$alerte = new alerte_mail();

$msg = "";
if (isset($_GET['mail']) && $_GET['mail']!='' && isset($_GET['salt']) && $_GET['salt']!='') {
	$ret = $alerte->activate($_GET['mail'], $_GET['salt']);
	if ($ret==true) {
		$alerte->reload();
		$alerte->sendAllResult();
		header('Location: ../alerte/activate.php?id=' . $alerte->id);
	} elseif ($ret=='already_activate') {
		$msg = "Cette alerte a déjà été activée.";
	} else {
		log_error("id manquant lors de l'ACTIVATION d'une alerte. [alerte.php]");
		$msg = "Une erreur est survenue : l'alerte n'a pas pu etre activee.";
	}
}

if (isset($_POST) && !empty($_POST))
{
	$errors = treatContactForm($_POST);
	if (empty($errors))
	{
		// enregistrement des criteres de recherche
		$data = annonce::treatForm($_POST);

		$alerte->type_transaction->id = $data['type_transaction_id'];
		$alerte->type_bien->id        = $data['type_bien_id'];
		$alerte->localisation->id     = $data['localisation_id'];
		$alerte->prix_mini            = $data['prix_mini'];
		$alerte->prix_maxi            = $data['prix_maxi'];
		$alerte->surface_mini         = $data['surface_mini'];
		$alerte->surface_maxi         = $data['surface_maxi'];
		$alerte->piece_mini           = $data['piece_mini'];
		$alerte->piece_maxi           = $data['piece_maxi'];

		$alerte->civilite = $CIVILITES[$_POST['civilite_id']];
		$alerte->nom = $_POST['nom'];
		$alerte->prenom = $_POST['prenom'];
		$alerte->mail = $_POST['mail'];
		
		//npr($alerte); exit();
		$alerte->save();
		$alerte->reload();

		if( !$alerte->sendMail('inscription') ) {
			echo "There has been a mail error sending to " . $alerte->mail . ".<br>";
		} else {
			//echo "A mail has been sent to " . $alerte->mail . " successfully.<br>";
			$_SESSION['search_annonces'] = $_POST;
			header('Location: save.php?alerte=' . $alerte->mail);
		}
	}
}
//npr($alerte);
?>
<?php $page='alerte'; require_once('../layout/header.php'); ?>

<div id="leftColumn">
	<h2>Enregistrez une alerte mail<span>pour reçevoir toutes nos nouvelles annonces</span></h2>
	<p><?php echo $msg; ?></p>
	<p class="justify">Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec congue nunc. Donec ipsum felis, dapibus et, mollis eget, egestas at, nulla. Cras suscipit iaculis nisl.</p>
	<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
		<?php $short=true; require_once('../public/contact.inc.php') ?>
		<?php require_once('../annonce/search.inc.php'); ?>
		<p class="submit">
			<input type="submit" class="submit" id="submit" name="submit" value="Enregistrer" />
		</p>
	</form>
</div>
<div id="rightColumn">
	<h3>vide</h3>
	<p>nada</p>
</div>

<?php require_once('../layout/footer.php'); ?>
